package org.vacoor.ui.qq.combobox.demo;

import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.StringTokenizer;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.vacoor.ui.qq.combobox.ItemListener;
import org.vacoor.ui.qq.combobox.QQComItem;
import org.vacoor.ui.qq.combobox.QQComboBox;

public class ComDemoFromFile {

	String userfile = "users.properties";
    Properties properties;

    public ComDemoFromFile() {
        //创建Properties对象，并加载文件        
        properties = new Properties();
        try {
            BufferedInputStream input = new BufferedInputStream(ComDemoFromFile.class.getResourceAsStream(userfile));
            properties.load(input);
            input.close();
        } catch (Exception e) {
            System.out.println("加载没有成功");
        }
    }

    public QQComboBox getUsersComboBox(boolean editable) {
        QQComItem[] mcbi;
        StringTokenizer token;
        String[] usersInfo;
        String num;
        String icon;
        String name;

        usersInfo = getUsersInfo();
        int size = usersInfo == null ? 0 : usersInfo.length;
        mcbi = new QQComItem[size];
        for (int i = 0; i < size; i++) {
            token = new StringTokenizer(usersInfo[i], ":");
            num = token.nextToken();
            icon = token.nextToken();
            name = token.nextToken();
            System.out.println(icon);
            mcbi[i] = new QQComItem(new ImageIcon(getClass().getClassLoader().getResource(icon)), name, num);
        }
        QQComboBox mcb = new QQComboBox(mcbi);
        mcb.setEditable(editable);
        return mcb;
    }

    private String[] getUsersInfo() {
        String[] result = null;

        //返回此属性列表中的键集字符串
        Set<String> set = properties.stringPropertyNames();
        //得到键集大小
        int size = set.size();
        if (size == 0) {
            return result;
        }
        result = new String[size];
        //将键集存入数组
        String[] values = set.toArray(new String[size]);
        for (int i = 0; i < size; i++) {
            String num = values[i];
            String info = properties.getProperty(num);
            //进行编码的转换
            try {
                //得到平台默认编码并转换
                //String code = java.nio.charset.Charset.defaultCharset().toString();
                //info = new String(info.getBytes(code));
                result[i] = num + ":" + info;
            } catch (Exception e) {
                System.out.println("异常" + e);
            }
        }
        return result;
    }

    public boolean delUsersInfo(String num) {
        boolean isDel = false;
        properties.remove(num);
        try {
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(userfile));
            properties.store(output, null);
            output.flush();
            output.close();
            isDel = true;
        } catch (FileNotFoundException ex) {
        } catch (IOException e) {
            System.out.println(e);
        }
        System.out.println("删除用户:" + num + " is " + isDel);
        return isDel;
    }

    public static void main(String[] args) {
        QQComboBox mcb;
        final ComDemoFromFile myComTest2 = new ComDemoFromFile();
        JPanel jp = new JPanel();
        mcb = myComTest2.getUsersComboBox(true);
        jp.add(mcb);
        mcb.setFieldIconShow(false);
//        mcb.setNoItemText("呵呵");
        mcb.addMItemListener(new ItemListener() {

            @Override
            public boolean itemDeleting(QQComItem item) {
                return true;
            }

            @Override
            public void itemDeleted(QQComItem item) {
                myComTest2.delUsersInfo(item.getNum());
            }

            @Override
            public void itemSelected(QQComItem item) {
            }
        });
        JFrame jf = new JFrame();
        jf.setUndecorated(true);
        jf.add(jp);
        jp.setBackground(Color.WHITE);
        jf.setVisible(true);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);
        jf.pack();
        jf.setAlwaysOnTop(true);
    }
}
